package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.backend.dto.Response;
import org.example.backend.dto.UserPreferenceDTO;
import org.example.backend.entity.UserPreference;
import org.example.backend.service.UserPreferenceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "用户偏好", description = "用户偏好管理相关接口")
@RestController
@RequestMapping("/preferences")
@RequiredArgsConstructor
public class UserPreferenceController {

    private final UserPreferenceService preferenceService;

    @Operation(summary = "添加偏好")
    @PostMapping
    public Response<UserPreference> addPreference(@Valid @RequestBody UserPreferenceDTO preferenceDTO) {
        return Response.success(preferenceService.addPreference(preferenceDTO));
    }

    @Operation(summary = "删除偏好")
    @DeleteMapping("/{categoryId}")
    public Response<Void> removePreference(@PathVariable Long categoryId) {
        preferenceService.removePreference(categoryId);
        return Response.success();
    }

    @Operation(summary = "获取当前用户的偏好")
    @GetMapping
    public Response<List<UserPreference>> getUserPreferences() {
        return Response.success(preferenceService.getUserPreferences());
    }

    @Operation(summary = "获取指定用户的偏好")
    @GetMapping("/users/{userId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<List<UserPreference>> getUserPreferences(@PathVariable Long userId) {
        return Response.success(preferenceService.getUserPreferences(userId));
    }
}
